const lodash = require('lodash');
const view = {

    init(tablename, detailSQL, requiredSQL, primaryKey) {
        let camelCaseTableName = lodash.camelCase(tablename);
        let camelCaseTableNameFirst = lodash.upperFirst(camelCaseTableName);
        let modelName = camelCaseTableName + "Model";
        let page = `
import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { Drawer, Form, Button, Col, Row, Descriptions } from 'antd';
import util from 'utils/util';

// 与model建立连接
@connect(({ ${modelName} }) => ({
    detailShowVisible: ${modelName}.detailShowVisible,
    detailData: ${modelName}.detailData
}))
class Detail extends PureComponent {

    constructor(props) {
        super(props);
        this.state = {
            //用于重新加载数据项
            fromKey: Math.random()
        };
    }

    componentDidMount() {
    }

    onClose = () => {
        const { dispatch } = this.props;
        dispatch({
            type: '${modelName}/updateState',
            payload: { detailShowVisible: false },
        });
    };

    render() {
        const { detailShowVisible,detailData } = this.props;
        const { fromKey } = this.state;
        const { onClose } = this;
        return (
            <div>
                <Drawer
                    title="详情"
                    width={720}
                    onClose={onClose}
                    visible={detailShowVisible}
                >
                <Descriptions column={2} bordered size="small">
                    ${getDescriptionsItems(detailSQL)}
                </Descriptions>
                </Drawer>
            </div>
        );
    }
}

export default Detail;`;
        return page;
    }

}

module.exports = view;

/**
 * 得到全部列表
 * @param {*} detailSQL 
 */
function getDescriptionsItems(detailSQL) {
    let ret = [];
    for (var i = 0; i < detailSQL.length; i++) {
        ret.push(`
        <Descriptions.Item label="${detailSQL[i].column_comment}">{detailData.${detailSQL[i].t_column_name}}</Descriptions.Item>`);
    }
    return ret.join("");
}

